package cn.springboot.erp.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Categories;
import cn.springboot.erp.exception.CustomerNameException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.springboot.erp.entity.Suppliers;
import cn.springboot.erp.service.SuppliersService;
import cn.springboot.erp.mapper.SuppliersMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
* @author asus
* @description 针对表【suppliers】的数据库操作Service实现
* @createDate 2024-05-07 16:01:07
*/
@Transactional

@Service
public class SuppliersServiceImpl extends ServiceImpl<SuppliersMapper, Suppliers>
    implements SuppliersService{

    @Override
    public ResponseResult listPage(QueryParams<Suppliers> queryParams) {
        // 获取分页数据
        Integer page = queryParams.getPage();
        Integer limit = queryParams.getLimit();
        Suppliers searchParams = queryParams.getSearchParams();

        // 数据分页
        Page<Suppliers> suppliersPage = new Page<>(page,limit);
        // 查询数据
        LambdaQueryWrapper<Suppliers> lambda = new QueryWrapper<Suppliers>().lambda();
        lambda.like(StringUtils.isNotBlank(searchParams.getName()),Suppliers::getName,searchParams.getName())
                .like(StringUtils.isNotBlank(searchParams.getContactName()),Suppliers::getContactName,searchParams.getContactName())
                .orderByDesc(Suppliers::getCreateTime);
        baseMapper.selectPage(suppliersPage,lambda);
        return ResponseResult.Success()
                .data("total",suppliersPage.getTotal())
                .data("supplierList",suppliersPage.getRecords());
    }

    @Override
    public ResponseResult importCategories(MultipartFile file) throws Exception {
        // 判断文件是否为空
        if(file.isEmpty()){
            return ResponseResult.Fail().msg("请选择文件");
        }

        // 获取输入流
        InputStream inputStream = file.getInputStream();
        // 设置导入参数
        ImportParams importParams = new ImportParams();
        // 设置表头行数
        importParams.setHeadRows(1);
        // 设置表格标题行数
        importParams.setTitleRows(1);
        // 获取导入数据
        List<Suppliers> suppliersList = ExcelImportUtil.importExcel(inputStream, Suppliers.class, importParams);
        System.out.println("----   suppliersList = " + suppliersList);
        // 将数据添加到数据库
        suppliersList.forEach(Supplier->{
            // 获取供应商名称
            String name = Supplier.getName();
            // 根据名称查询数据库
            LambdaQueryWrapper<Suppliers> lambda = new QueryWrapper<Suppliers>().lambda();
            lambda.eq(Suppliers::getName,name);
            Suppliers one = baseMapper.selectOne(lambda);
            if (ObjectUtils.isNotNull(one)){
                throw new CustomerNameException("该供应商已经存在："+name);
            }
            // 添加供应商信息
            baseMapper.insert(Supplier);
        });
        return ResponseResult.Success().msg("批量导入成功");
    }

    @Override
    public void exportSuppliers(Map<String, Object> params, HttpServletResponse response) throws IOException {
        // 获取参数
        Object name = params.get("name");
        Object contactName = params.get("contactName");
        // 查询数据
        LambdaQueryWrapper<Suppliers> lambda = new QueryWrapper<Suppliers>().lambda();
        lambda.like(ObjectUtils.isNotNull(name),Suppliers::getName,name)
                .like(ObjectUtils.isNotNull(contactName),Suppliers::getContactName,contactName)
                .orderByDesc(Suppliers::getContactName);
        List<Suppliers> supplierList = baseMapper.selectList(lambda);

        // 获取输出流
        ServletOutputStream outputStream = response.getOutputStream();
        // 设置批量导出参数
        ExportParams exportParams = new ExportParams();
        exportParams.setTitle("供应商信息表");
        exportParams.setSheetName("供应商信息");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Suppliers.class, supplierList);
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }
}




